define(function(require, exports, module) {
	// 通过 require 引入依赖
	var $ = require("jQuery");
	var ajax = require("../common/util/ajax");
	require('../common/util/ajaxExt');
	var Handlebars = require("handlebars");
	var helpers = require("../common/helpers");
	Handlebars.registerHelper(helpers);
	require("./teaQuestionList.css");
	require("../common/common.css");
	var moment = require("moment");
	var Util = require("../common/util/util");
	require("../common/ellipsis/ellipsis");
	require("../common/common_require");

	var current = 1;
	var length = 10;

	$('input').on('ifChecked', function(e) {
		$("#q_list").bootstrapTable('refresh', {
			url : '/qaplus/q/query_s_question.json'
		});
	});
	$('input').on('ifUnchecked', function(e) {
		$("#q_list").bootstrapTable('refresh', {
			url : '/qaplus/q/query_s_question.json'
		});
	});

	var questionList = {
		initPage : function() {
			$('.question_search input').each(function() {
				var self = $(this);
				var label = self.next();
				var label_text = label.text();
				label.remove();
				self.iCheck({
					checkboxClass : 'icheckbox_sm-blue',
					radioClass : 'radio_sm-blue',
					insert : label_text
				});
			});
		},
		initTableData : function() {
			$('#q_list').bootstrapTable(
					{
						url : "/qaplus/q/query_s_question.json",
						method : 'post',
						height : 600,
						cache : false,
						dataType : "json",
						contentType : "application/json",
						sidepagination : "server", // 服务端请求
						queryParamsType : "limit",
						cache : false, // 是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
						queryParams : questionList.queryParams,// 传递参数（*）
						detailView : true,
						rowStyle : function(row, index) {
							var classes = [ 'active', 'success', 'info',
									'warning', 'danger' ];
							if (row.needFaceComm == 'Y') {
								return {
									classes : classes[4]
								};
							}
							return {};
						},
						detailFormatter : questionList.detailFormatter,
						sidePagination : "server",
						pagination : true,
						paginationPreText : "上一页",
						paginationNextText : "下一页",
						pageSize : 10,
						pageList : [ 10, 25, 50 ],
						responseHandler : questionList.responseHandler,
						striped : true,
						
						search : true,
						formatSearch:function(){
							return "输入标题模糊搜索";
						},
						searchText:Util.getUrlParam("title"),
						
						
						showFooter:true,
						formatNoMatches : function() {
							return '客官，对不起，没有符合条件的记录';
						},
						onLoadSuccess : function(result) {
							// 解决：标题或者内容太长的情况
							$('#q_list .ellip').ellipsis({
								maxLine : 1,
								maxWidth : 200,
							});
							$(".answer").click(function(){
								questionList.answer($(this).data("id"));
							});
							$(".isSolved").click(function(){
								questionList.isSolved($(this).data("id"));
							});
							$(".attach").click(function(){
								Util.downloadAttach($(this).data("id"));
							});
							
						},
						columns : [
								{
									field : 'i',
									width : '2%',
									title : '序号',
									formatter : function(value, row, index) {
										return index + 1;
									}
								},
								{
									field : 'title',
									title : '问题标题',
									width : "15%",
									class : "ellip",
									footerFormatter:questionList.footerFormatter,
								},
								{
									field : 'questionContent',
									title : '问题描述',
									width : "15%",
									class : "ellip"
								},
								{
									field : 'recUpdateDate',
									width : "10%",
									title : '提问时间',
									formatter : function(value, row, index) {
										return moment(value).format(
												"YYYY-MM-DD");
									}
								},
								{
									field : 'solveTime',
									width : "15%",
									title : '解决时间',
									titleTooltip : "点击左边的'+'查看详细信息",
									formatter : function(value, row, index) {
										if (value) {
											return moment(value).format(
													"YYYY-MM-DD");
										} else {
											return value;
										}
									}
								},
								{
									field : 'questionPerson',
									width : "10%",
									title : '提问学生'
								},
								{
									field : 'isSolve',
									width : "10%",
									title : '试题状态',
									formatter : function(value, row, index) {
										return value == "N"
												|| value == undefined ? "未解决"
												: "已解决";
									}
								}, {
									field : "questionHasAttachment",
									visible : false,
								}, {
									title : '操作',
									field : 'id',
									width : "15%",
									formatter : questionList.operateFormatter
								} ]
					});
		},
		queryParams : function(params) {
			return $("#searchForm").serialize()+"&begin="+params.offset+"&end="+params.limit+"&title="+params.search;
		},
		responseHandler : function(result) {
			if (result.hasError) {
				layer.msg(result.msg, {
					// 关闭后的操作
					icon : 5
				});
				return {
					"rows" : [],
					"total" : 0
				};
			} else {
				return {
					"rows" : result.data,
					"total" : result.count
				};
			}
		},
		detailFormatter : function(index, row) {
			var html = [];
			$.each(row, function(key, value, index) {
				if (key == 'title' || key == 'questionContent'
						|| key == 'recCreateDate' || key == "solveTime"
						|| key == "questionPerson") {
					var val = "";
					if (key == 'title') {
						val = "问题标题";
					} else if (key == 'questionContent') {
						val = "问题描述";
					} else if (key == 'recCreateDate') {
						val = "提问时间";
					} else if (key == 'solveTime') {
						val = "解决时间";
					} else if (key == 'questionPerson') {
						val = "提问学生";
					}
					if(!value){
						value="";
					}
					html.push('<p><b>' + val + ':</b> ' + value + '</p>');
				}
			});
			return html.join('');
		},
		operateFormatter : function(value, row, index) {
			var d = "";
			var a = "";
			var f = "";
			if (row.isSolve == "N" || row.isSolve == undefined) {
				d = '<span class="label label-success answer" style="cursor:pointer;" data-id='+row.id+'>回答</span>&nbsp;';
				a = '<span class="label label-danger isSolved" style="cursor:pointer;" data-id='+row.id+'>已解决</span>&nbsp;'
			}
			if (row.questionHasAttachment == "Y") {
				f = '<span class="label label-info attach" style="cursor:pointer;" data-id='+row.questionAttachmentId+'>附件</span>&nbsp;';
			}
			return d + a + f;
		},
		footerFormatter:function(data){
			console.log(data);
			return "";
		},
		isSolved : function(id) {
			// 询问框
			layer.confirm('确定已解决这个问题？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				ajax({
					url : "/qaplus/q/upload_a.json",
					data : "id=" + id,
					success : function(result) {
						window.location.reload(true);
					},
				});
			}, function(index) {
				layer.close(index);
			});
		},
		answer : function(id) {
			window.open("/qaplus/teaAnswerQuestion/teaAnswerQuestion.jsp?id=" + id, "教师回答",
					null, null);
		}
	};

	// 输出接口，让boot.js启动
	module.exports = {
		init : function() {
			questionList.initPage();
			questionList.initTableData();
		}
	};
});
